import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pizza_test/model/chefmodel.dart';

class PizzPropertyView extends StatefulWidget {

  final Pizza pizza;
  final Function pizzaSizeAction;
  final Function chooseOtherAction;

  PizzPropertyView({Key key, this.pizza, this.pizzaSizeAction, this.chooseOtherAction}) : super(key: key);

  @override
  _PizzPropertyViewState createState() => _PizzPropertyViewState();
}

class _PizzPropertyViewState extends State<PizzPropertyView> {
  final selectedColor = 0xfffae29a;
  EpizzaSize size;
  initState(){
    super.initState();
    size = widget.pizza.size;
  }
  Widget _createSize() {
    return Container(
              child: Row(
                children: <Widget>[
                  Container(
                    height: 40,
                    decoration: BoxDecoration(
                      color: Color(widget.pizza.size == EpizzaSize.small ? selectedColor : 0xfff6f6f6),
                      borderRadius: BorderRadius.horizontal(left: Radius.circular(20))
                    ),
                    child: FlatButton(
                      child: Text("SMALL"),
                      onPressed: (){
                        setState(() {
                          size = EpizzaSize.small;
                        });
                        widget.pizzaSizeAction(EpizzaSize.small);
                      },
                    ),
                  ),
                  Container(
                    height: 40,
                    color: Color(size == EpizzaSize.medium ? selectedColor : 0xfff6f6f6),
                    child: FlatButton(
                      child: Text("MEDIUM"),
                      onPressed: (){
                        setState(() {
                          size = EpizzaSize.medium;
                        });
                        widget.pizzaSizeAction(EpizzaSize.medium);
                      },
                    ),
                  ),
                  Container(
                    height: 40,
                    decoration: BoxDecoration(
                      color: Color(widget.pizza.size == EpizzaSize.large ? selectedColor : 0xfff6f6f6),
                      borderRadius: BorderRadius.horizontal(right: Radius.circular(20))
                    ),
                    child: FlatButton(
                      child: Text("LARGE"),
                      onPressed: (){
                        setState(() {
                          size = EpizzaSize.large;
                        });
                        widget.pizzaSizeAction(EpizzaSize.large);
                      },
                    ),
                  ),
                ],
              ),
            );
  }

  List<Widget> _createButton() {
    List<String> list = ["roast_beef",
    "bell_peppers",
    "mushrooms",
    "onions",
    "tomatoes",
    "marinara",];
    list = list.map((val) => val.toUpperCase()).toList();
    list.add("+More Topping");
    List<Widget> res = [];
    list.forEach((val){
      res.add(
        SelectedButton(title: val, isselect: widget.pizza.toppings.contains(val), chooseOtherAction: widget.chooseOtherAction,)
      );
    });

    return res;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Container(
          height: 400,
          width: 500,
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10))
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text("SIZE", style: TextStyle(color: Colors.grey),),
              SizedBox(height: 15),
              _createSize(),
              SizedBox(height: 15),
              Text("Topping", style: TextStyle(color: Colors.grey),),
              Wrap(
                spacing: 5,
                children: _createButton(),
              ),
              Expanded(
                child: Container(),
              ),
              Container(
                alignment: Alignment.center,
                child: IconButton(
                  icon: Icon(Icons.close),
                  onPressed: (){
                    Navigator.of(context).pop();
                  },
                ),
              )
            ],

          ),
        ),
      ),
    );
  }
}


class SelectedButton extends StatefulWidget {

  final String title;
  final bool isselect;
  final Function chooseOtherAction;

  const SelectedButton({Key key, this.title, this.isselect, this.chooseOtherAction}) : super(key: key);

  @override
  _SelectedButtonState createState() => _SelectedButtonState();
}

class _SelectedButtonState extends State<SelectedButton> {

  final selectedColor = 0xfffae29a;
  bool isselect;
  @override
  void initState() {
    super.initState();
    isselect = widget.isselect;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
            height: 40,
            margin: const EdgeInsets.only(top:10),
            decoration: BoxDecoration(
              color: Color(isselect ? selectedColor : 0xfff6f6f6),
              borderRadius: BorderRadius.all(Radius.circular(20))
            ),
            child: FlatButton(
              child: Text(widget.title),
              onPressed: (){
                widget.chooseOtherAction(widget.title);
                setState(() {
                  isselect = !isselect;
                });
              },
            ),
          );
  }
}